package com.example.okhttp;

import com.alibaba.fastjson.JSON;

import java.io.UnsupportedEncodingException;

/**
 * @author:0.2℃
 * @date： 2022-01-05 06:59
 * @version:
 * @Description ：将我们的JsonHttpRequest，CallbackListener全部被封装到一起
 * 原因：因为队列里面放一个对象是最好的所以这个类既包含请求也包含响应
 */
public class HttpTask<T> implements Runnable{
    IHttpRequest httpRequest;
    public HttpTask(String url, T requestData, IHttpRequest httpRequest, CallbackListener listener) {
        this.httpRequest = httpRequest;
        httpRequest.setURL(url);
        httpRequest.setListener(listener);
        String content = JSON.toJSONString(requestData);
        try {
            httpRequest.setData(content.getBytes("UTF-8"));
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }
    }

    @Override
    public void run() {
        this.httpRequest.execute();
    }
}
